import sys
from unittest.mock import patch

import pytest

from pdm_api_frame.main import get_file_with_path, my_sum


def test_get_file_with_path():
    """
    Test get_file_with_path function
    """
    file_list = get_file_with_path(
        "D:\ggl-automation-frame\pdm_api_frame\src\pdm_api_frame"
    )

    assert len(file_list) == 2

    assert (
        "D:\ggl-automation-frame\pdm_api_frame\src\pdm_api_frame\main.py" in file_list
    )


@pytest.mark.parametrize(
    "a,b,c,choice",
    [
        ("3", "4", "7", "sum"),
        ("", "4", "对不起，请输入两个数字！", "sum"),
        ("4", "", "对不起，请输入两个数字！", "sum"),
        ("ads", "adf", "对不起，请输入数字！", "sum"),
        ("&*(*(&", ")$%$#", "对不起，请输入数字！", "sum"),
        ("3", "4", "-1", "sub"),
        ("", "4", "对不起，请输入两个数字！", "sub"),
        ("4", "", "对不起，请输入两个数字！", "sub"),
        ("ads", "adf", "对不起，请输入数字！", "sub"),
        ("&*(*(&", ")$%$#", "对不起，请输入数字！", "sub"),
        ("3", "4", "12", "mul"),
        ("", "4", "对不起，请输入两个数字！", "mul"),
        ("4", "", "对不起，请输入两个数字！", "mul"),
        ("ads", "adf", "对不起，请输入数字！", "mul"),
        ("&*(*(&", ")$%$#", "对不起，请输入数字！", "mul"),
        ("4", "4", "1", "div"),
        ("", "4", "对不起，请输入两个数字！", "div"),
        ("4", "", "对不起，请输入两个数字！", "div"),
        ("ads", "adf", "对不起，请输入数字！", "div"),
        ("&*(*(&", ")$%$#", "对不起，请输入数字！", "div"),
    ],
)
def test_my_sum(capsys, a, b, c, choice):
    """
    1、调my_sum()方法
    2、在标准输入中输入两个数字 3 4
    3、在标准输出中，获取计算结果
    4、断言，获取的结果和预期的结果相同
    :return:
    """
    sys.argv = ["main.py", choice]
    # mock猴子补丁，模拟标准输入
    with patch("builtins.input", side_effect=[a, b]):
        my_sum()

    out, err = capsys.readouterr()

    flag = ""
    if choice == "sum":
        flag = "相加"
    elif choice == "sub":
        flag = "相减"
    elif choice == "mul":
        flag = "相乘"
    elif choice == "div":
        flag = "相除"

    if "对不起" in c:
        assert c in out
    else:
        assert f"两数{flag}的结果为： {c}" in out
